//
//  GKZYReferenceListController.m
//  GKZYAid
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYReferenceListController.h"
#import "GKZYReferenceCell.h"
#import "DatabaseRequest.h"
#import "Urls.h"
#import "GKZYMessageModel.h"
#import "GKZYReferenceDetailController.h"
#import "MJRefresh.h"
@interface GKZYReferenceListController ()<UITableViewDataSource,UITableViewDelegate>

//存放备考数据的数组:
@property (nonatomic,strong)NSMutableArray *referenceDataArray;

//展示条数:
@property (nonatomic,assign)NSInteger n;

@end

@implementation GKZYReferenceListController

- (void)loadView{
    
    self.referenceView = [[GKZYReferenceView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.referenceView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置n的初始值:
    self.n = 0;
    
    //注册cell:
    [self.referenceView.tableView registerClass:[GKZYReferenceCell class] forCellReuseIdentifier:@"referenceCELL"];
    
    //设置代理:
    [self setDelegate];
    
    //请求数据:
    [self makeData];
    
    //上下拉刷新:
    [self setupRefresh];
    
    
    
}
- (void)setupRefresh
{
    
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.referenceView.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
#warning 自动刷新(一进入程序就下拉刷新)
    [self.referenceView.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.referenceView.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.referenceView.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    self.referenceView.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.referenceView.tableView.headerRefreshingText = @"MJ哥正在帮你刷新中,不客气";
    
    self.referenceView.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.referenceView.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.referenceView.tableView.footerRefreshingText = @"MJ哥正在帮你加载中,不客气";
}

//下拉刷新:
- (void)headerRereshing{
    
    if (self.n > 0) {
        
        self.n --;
        
        [self makeData];
        
        [self.referenceView.tableView headerEndRefreshing];
        
    }else{
        
        self.referenceView.tableView.headerRefreshingText = @"已经是最新数据了";
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.referenceView.tableView headerEndRefreshing];
        });
        
        
    }
    
    
}

//上拉加载:
- (void)footerRereshing{
    
    if (self.n < 30) {
        
        self.n ++;
        
        [self makeData];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.referenceView.tableView footerEndRefreshing];
            
        });
        
        
    }else{
        
        self.referenceView.tableView.footerRefreshingText = @"MJ哥正在帮你加载中,不客气";
    }
    
    
    
}


- (void)makeData{
    
    [DatabaseRequest requestMessageUrlString:[NSString stringWithFormat:KReference,self.n * 30] didFished:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableDictionary *dicRes = dict[@"res"];
        NSMutableArray *arrayNewsList = dicRes[@"news_list"];
        
        if (self.referenceDataArray != nil) {
            
            [self.referenceDataArray removeAllObjects];
        }
        
        for (NSDictionary *dicModel in arrayNewsList) {
            
            GKZYMessageModel *model = [[GKZYMessageModel alloc] init];
            [model setValuesForKeysWithDictionary:dicModel];
            [self.referenceDataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.referenceView.tableView reloadData];
        });
        
    }];
    
}

//懒加载初始化:
- (NSMutableArray *)referenceDataArray{
    
    if (!_referenceDataArray) {
        
        _referenceDataArray = [[NSMutableArray alloc]init];
    }
    
    return _referenceDataArray;
}

- (void)setDelegate{
    
    self.referenceView.tableView.delegate = self;
    self.referenceView.tableView.dataSource = self;
    
}


#pragma mark ------------ 实现UITableView代理方法-----------------
//返回多少个分区:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

//返回分区下的行数:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.referenceDataArray.count;
}

//设置cell"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GKZYReferenceCell *cell = [self.referenceView.tableView dequeueReusableCellWithIdentifier:@"referenceCELL"];
    cell.model = self.referenceDataArray[indexPath.row];
    
    return cell;
}

//返回行高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

//点击cell事件:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GKZYReferenceDetailController *referenceDetailVC = [GKZYReferenceDetailController new];
    GKZYMessageModel *model = self.referenceDataArray[indexPath.row];
    referenceDetailVC.modelDetail = model;
    
    [self.navigationController pushViewController:referenceDetailVC animated:YES];
}

//返回分区头视图的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}


//返回分区尾部视图的高度;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
